const { ipcRenderer } = require("electron");
const { readFile } = require('fs/promises')
const buffer = require("node:buffer")
const path = require('path')
window.ipcRenderer = ipcRenderer;
window.readFile = readFile


window.pathParse = (filePath) => {
  let file = path.parse(filePath);
  let ext = file.ext.slice(1)
  return [file.name, ext]
}

window.toBuffer = (bf) => {
  return buffer.Buffer.from(bf)
}


// index.html
window.alwaysOnTop = () => {
  ipcRenderer.sendTo(window.senderId, "win", 'alwaysOnTop');
  let data = getData()
  window.sendSaveData(data)
};

window.isAlwaysOnTop = () => {
  ipcRenderer.sendTo(window.senderId, "win", "isAlwaysOnTop");
};

window.getPosition = () => {
  ipcRenderer.sendTo(window.senderId, "win", 'position');
}

window.moveDirection = (type, num) => {
  ipcRenderer.sendTo(window.senderId, "direction", {type, num});
}

window.sendSaveData = (data) => {
  ipcRenderer.sendTo(window.senderId, "saveData", data);
}


ipcRenderer.on('senderId', (event) => {
  window.senderId = event.senderId;
})

window.closeWin = () => {
  if(window.removeImgBuffer){
    window.removeImgBuffer();
  }
  if(window.closeSettingWin){
    window.closeSettingWin()
  }
  ipcRenderer.sendTo(window.senderId, "win", "close");
};


// 向父页面传送数据
window.toParentPage = (data, info) => {
  ipcRenderer.sendTo(window.senderId, 'toParent', {data, ...info});
}

